<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
      /*  $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover,{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');*/
      //echo "hello yibu";
      $this->display();
    }


    public function register()
    {
    	  $userModel=D('Users');
        if($userModel->create()){

       
                   if($_FILES['photo']['error']==0)
                    {
                        $fileInfo=$this->img();
                        $userModel->photo=$fileInfo;
                    }
                    if(  $userModel->add() )
                    {
                        //var_dump($_POST);
                        $this->success('添加成功',U('Admin/Index/users'));
                    }
                    else
                    {
                        //var_dump($_POST);
                        $this->error('失败','users');
                    }

                }
        else
        {
            //$this->error("111111111");
             $this->error($userModel->getError());
        }

    }



      public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
       // $upload->savePath = ''; // 设置附件上传（子）目录
        // 上传文件
        $upload->autoSub=false;
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
        $this->error($upload->getError());
        }else{// 上传成功
       // $this->success('上传成功！');
            return $info;
        }
    }


    function img()
    {
        if($_FILES['photo']['error']==0)
        {


           $photoinfo=$this->upload();//var_dump($photoinfo);
            $fillPath='./Public/Uploads/photo/';
             $filePath=$photoinfo['photo']['rootPath'];
            $oldFileName=$photoinfo['photo']['savename'];
            $oldFileFullName=$fillPath."\\".$oldFileName;
            $newFileName='thumb_'.$oldFileName;
            $newFileFullName=$fillPath."\\".$newFileName;

           
           $img=new \Think\Image();
           $img->open($oldFileFullName);
           $img->thumb(100,100)->save($newFileFullName);
           return $newFileName;

        }
    }

// /    public 
//     {
//     	$this->display();
//     }
}